﻿using RAP.Framework.Libary.DomTemplateBuilder.Attributes;
using RAP.Framework.Libary.DomTemplateBuilder.Handler;
using RAP.Framework.Libary.Utils;

namespace RAP.Framework.Libary.DomTemplateBuilder.Element
{
	/// <summary>
	/// 
	/// </summary>
	[TagHandler( typeof( ExpressionHandler ) )]
    public class Expression : SimpleText
    {
        private bool? _ignoreEmpty = null;

        /// <summary>
        /// 是否允许忽略空表达式，如果不允许忽略，则在表达式为空时抛出异常
        /// </summary>
        public bool IgnoreEmpty
        {
            get
            {
                if ( _ignoreEmpty == null )
                {
                    _ignoreEmpty = this.ToBoolean( "IgnoreEmpty", true );
                }
                return _ignoreEmpty.Value;
            }
        }

        private string _errorMessage = null;

        /// <summary>
        /// 表达式执行出错时抛出的异常信息
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                if ( _errorMessage == null )
                {
                    _errorMessage = this.ToString( "ErrorMessage", "表达式语法错误！" );
                }
                return _errorMessage;
            }
        }
        
        public override void Initialize ( System.Xml.XmlNode node )
        {
            var parentEle = this.Parent as IExpressionElement;
            if ( parentEle != null && parentEle.ExpressionElement == null )
            {
                //如果Expression节点实在IExpressionElement节点下，则作为特殊子节点处理
                parentEle.ExpressionElement = this;
            }
            else
            {
                //如果父节点不是IExpressionElement类型或者父节点的ExpressionElement属性已存在，* 则当前表达式节点作为普通节点处理
                base.Initialize( node );
            }
        }
    }
}
